Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix vertical scaling #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

magnusdv
Copy link

Hi,

This PR modifies 2 lines of plot.pedigree(), fixing a minor bug affecting the vertical scaling. The problem is most visible when plotting small pedigrees with multiline labels:

library(kinship2, quietly = TRUE)

# Simple trio with multiline labels
x = pedigree(1:3, dadid = c(0,0,1), momid = c(0,0,2), sex = c(1,2,1))
id = c("1", "2", "3\n1/1\n1/1\n1/1\n1/1\n1/1\n1/1")

# Plot
par(mar = c(2,2,2,2))
plot(x, id = id)

# Show plot region
box("figure")
box("plot", lty = 2)    

Created on 2022-09-15 with reprex v2.0.2

As seen, the symbols are vertically squished, and the pedigree does not fill the plot region. This is caused by the following line in the source code, where the usr parameter is set:

kinship2/R/plot.pedigree.R

Lines 154 to 155 in 0a0dbd3

par(usr=c(xrange[1]- boxw/2, xrange[2]+ boxw/2,
maxlev+ boxh+ stemp3 + stemp2/2 , 1))

In the third entry, maxlev and boxh are correctly given in user coordinates, but the stemp3 and stemp2 terms are not -- they should be divided by vscale.

Furthermore, I believe that vscale is slightly miscalculated:

vscale <- (psize[2]-(stemp3 + stemp2/2 + boxsize))/ max(1, maxlev-1)

As I understand it, the term stemp2/2 (both here and in the usr vector) is meant to account for the vertical gap between the symbol and the label. But this gap is actually stemp2 * 0.7 (i.e., labh * 0.7 after scaling):

text(plist$pos[i,j], i + boxh + labh*.7, id[k], cex=cex,

Thus, the factor 1/2 is too small, sometimes allowing the label to go below the plot region. I suggest to simply remove it, which ensures a tiny amount of white space below the label.

Here is the same example plotted with the suggested changes:

devtools::install_github("magnusdv/kinship2", ref = "fix-scaling", quiet = TRUE)
library(kinship2, quietly = TRUE)

plot(x, id = id)

# Show plot region
box("figure")
box("plot", lty = 2)    

Created on 2022-09-15 with reprex v2.0.2

Thanks for maintaining this useful package!
Magnus

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant